package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.ClassMarketPurchaseActivity;
import cn.xfdzx.android.apps.shop.activity.RetailLevelActivity;
import cn.xfdzx.android.apps.shop.bean.CategoryAllBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：
 */
public class TypeMainAdapter extends RecyclerView.Adapter<TypeMainAdapter.MyViewHolder>{

    private List<CategoryAllBeen.DataBean> mList;
    private Context mContext;

    private int type;


    public void setData(List<CategoryAllBeen.DataBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public TypeMainAdapter(Context context,int type) {
        this.mContext = context;
        this.type = type;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_view, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull  MyViewHolder holder,  int i) {


        holder.rvView.setItemAnimator(new DefaultItemAnimator());
        GridLayoutManager mLayoutManager = new GridLayoutManager(mContext, 3);
        holder.rvView.setLayoutManager(mLayoutManager);
        holder.rvView.setNestedScrollingEnabled(false);
        TypeMainItemAdapter typeMainItemAdapter = new TypeMainItemAdapter(mContext);
        typeMainItemAdapter.setData(mList.get(i).getChild());
        typeMainItemAdapter.setOnItemClickListener(new  ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (type==1){
                    Intent intent = new Intent(mContext, RetailLevelActivity.class);
                    intent.putExtra("title_name", mList.get(i).getName());
                    intent.putExtra("type_id",  mList.get(i).getId()+"");
                    intent.putExtra("index",  position);
                    mContext.startActivity(intent);
                }else {
                    Intent intent=new Intent(mContext, ClassMarketPurchaseActivity.class);
                    intent.putExtra("title_name", mList.get(i).getChild().get(position).getName());
                    intent.putExtra("id", mList.get(i).getChild().get(position).getId()+"");
                    mContext.startActivity(intent);
                }
            }
        });
        holder.rvView.setAdapter(typeMainItemAdapter);

    }



    @Override
    public int getItemCount() {
        return mList==null?0:mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.rv_view)
        RecyclerView rvView;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }






}
